import {PlotTypes} from '../../enum/PlotTypes';
import {Point} from '../../type';

// @ts-ignore
export default class SegmentLine extends ol.geom.LineString {
    public params: any;
    public isPlot: boolean = true;
    public freehand: boolean;
    public fixPointCount: number = 2;

    private readonly _type: PlotTypes;
    private points: Array<Point>;

    constructor(coordinates: any, points: Array<Point>, params?: any) {
        super([]);
        this._type = PlotTypes.SEGMENT_LINE;///
        this.points = [];
        this.freehand = false;
        this.params = params;
        if (points && points.length > 0) {
            this.setPoints(points);
        } else if (coordinates && coordinates.length > 0) {
            // @ts-ignore
            this.setCoordinates(coordinates);
        }
    }

    /**
     * 获取标绘类型
     * @return {PlotTypes}
     */
    public getPlotType(): PlotTypes {
        return this._type;
    }

    /**
     * 获取坐标点
     * @return {Array<Array<number>>}
     */
    public getPoints(): Array<Point> {
        return this.points.slice(0);
    }

    /**
     * 获取点数理
     * @return {number}
     */
    public getPointCount(): number {
        return this.points.length;
    }

    /**
     * 更新当前坐标
     * @param point
     * @param index
     */
    public updatePoint(point: Point, index: number): void {
        if (index >= 0 && index < this.points.length) {
            this.points[index] = point;
            this.generate();
        }
    }

    /**
     * 更新最后一个坐标点
     * @param point
     */
    public updateLastPoint(point: Point): void {
        this.updatePoint(point, this.points.length - 1);
    }

    /**
     * 完成绘制
     */
    public finishDrawing(): void {

    }

    private setPoints(value: Array<Point>): void {
        this.points = !value ? [] : value;
        if (this.points.length >= 1) {
            this.generate();
        }
    }

    private generate(): void {
        // @ts-ignore
        this.setCoordinates(this.points);
    }
}